package tech.cn.note.html.tool;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import freemarker.ext.beans.StringModel;
import freemarker.template.SimpleDate;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateModelException;
import org.apache.commons.lang3.StringUtils;

import org.springframework.stereotype.Component;

/**
 * freeMarker针对LocalDate日期处理
 * 根据不同的日期类型转换为年，月，日
 *
 * @author liuyong
 */
@Component
public class DocumentDateConvert implements DocumentConvert {
    //转换器名称
    private static String CONVERT_NAME = "dateFormat";

    public static String YEAR_FORMAT = "YYYY";

    public static String MONTH_FORMAT = "MM";

    public static String DAY_FORMAT = "DD";

    @Override
    public String getConvertName() {
        return CONVERT_NAME;
    }

    @Override
    public Object exec(List params) throws TemplateModelException {
        if (params == null || params.size() < 1) {
            return "";
        }
        Object objParam = params.get(0);
        if (objParam == null || StringUtils.isBlank(objParam.toString())) {
            return "";
        }
        String strFormatter = DAY_FORMAT;
        if (params.size() > 1 && params.get(1) != null
                && StringUtils.isNotBlank(params.get(1).toString())) {
            strFormatter = params.get(1).toString();
        }

        //判断freemarker中的数据类型，根据不同的类型进行转换
        Object dateObj = null;
        if (objParam instanceof StringModel) {
            dateObj = ((StringModel) objParam).getWrappedObject();
        } else if (objParam instanceof SimpleDate) {
            dateObj = ((SimpleDate) objParam).getAsDate();
        } else if (objParam instanceof SimpleScalar) {
            return convertLocalDate(objParam.toString(), strFormatter);
        } else {
            throw new TemplateModelException("模板自定义日期转换无法识别模板中的数据类型");
        }
        //根据日期类型进行验证
        if (dateObj instanceof LocalDate) {
            return convertLocalDate((LocalDate) dateObj, strFormatter);
        } else if (dateObj instanceof Date) {
            return convertLocalDate((Date) dateObj, strFormatter);
        } else {
            throw new TemplateModelException("模板自定义日期转换无法识别的日期类型");
        }
    }

    /**
     * 转换字符串日期
     *
     * @param strDate
     * @param format
     * @return
     */
    public String convertLocalDate(String strDate, String format) {
        return convertLocalDate(LocalDate.parse(strDate), format);
    }

    /**
     * 转换LocalDate 日期
     *
     * @param date
     * @param format
     * @return
     */
    public String convertLocalDate(LocalDate date, String format) {
        if (YEAR_FORMAT.equals(format.toUpperCase())) {
            return String.valueOf(date.getYear());
        } else if (MONTH_FORMAT.equals(format.toUpperCase())) {
            return String.valueOf(date.getMonthValue());
        } else if (DAY_FORMAT.equals(format.toUpperCase())) {
            return String.valueOf(date.getDayOfMonth());
        } else {
            return DateTimeFormatter.ofPattern(format).format(date);
        }
    }

    /**
     * 转换Date 日期
     *
     * @param date
     * @param format
     * @return
     */
    public String convertLocalDate(Date date, String format) {
        //转换Calendar进行获取年月
        Calendar dataCal = Calendar.getInstance();
        dataCal.setTime(date);
        if (YEAR_FORMAT.equals(format.toUpperCase())) {
            return String.valueOf(dataCal.get(Calendar.YEAR));
        } else if (MONTH_FORMAT.equals(format.toUpperCase())) {
            return String.valueOf(dataCal.get(Calendar.MONTH) + 1);
        } else if (DAY_FORMAT.equals(format.toUpperCase())) {
            return String.valueOf(dataCal.get(Calendar.DATE));
        } else {
            return DateTimeFormatter.ofPattern(format).format(date.toInstant());
        }
    }
}
